from math import radians

from fastapi import APIRouter, Depends
# from geoalchemy2 import func
from sqlalchemy.orm import Session
from app.core.responses import Response
from app.database import engine
from app.models import Users
from app.schemas.user import UserInfo, UserNearbyList, Item, UserHistoryLocationSM
from app.services.auth import get_current_user
from app.services.user import UserServices

router = APIRouter()


@router.post("/user", description='编辑用户信息')
def create(form: UserInfo, user: str = Depends(get_current_user), ):
    UserServices.edit_user_info(user.id, form)
    return Response.success({}, message='编辑成功')


@router.get("/user_nearby_list", description='获取附近的人用户')
def nearby_list(latitude: float, longitude: float, page: int = 1, page_size: int = 1,
                user: str = Depends(get_current_user)):
    user_list = UserServices.nearby_users(latitude=latitude, longitude=longitude, page=page, page_size=page_size)
    items = [Item(**user.__dict__) for user in user_list['list']]
    return Response.success(UserNearbyList(list=items, total=user_list['total']))


@router.post("/user_location", description='添加定位信息')
def location_store(form: UserHistoryLocationSM,
                   user: str = Depends(get_current_user)):
    if UserServices.location_history_store(user.id, form):
        UserServices.edit_user_info(user.id, form)
        return Response.message('添加成功')
    return Response.failed('失败！')
